package MD.NJavaEyu;

import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

import com.appsflyer.AppsFlyerConversionListener;
import com.easyplay.shooting.R;
import com.eyu.common.SdkHelper;
import com.eyu.common.ad.EyuAdManager;
import com.eyu.common.ad.EyuAdsListener;
import com.eyu.common.ad.model.AdConfig;
import com.eyu.common.ad.model.TestParams;
import com.eyu.common.firebase.EventHelper;
import com.eyu.common.firebase.EyuRemoteConfigHelper;
import com.google.android.gms.ads.identifier.AdvertisingIdClient;

import java.util.HashMap;
import java.util.Map;

import MD.NJavaBase.IActivityOverride;
import MD.NJavaBase.NJavaBase;
//文档地址 https://github.com/EyugameQy
public class NJavaEyu {
    static final  String Tag = "NJavaEyu";
    public static void initBase()
    {
        Activity activity =  NJavaBase.getActivity();

        SdkHelper.init(activity);

        NJavaBase.addActivityOverride(new IActivityOverride() {
            @Override
            public void onResume() {
                Activity activity =  NJavaBase.getActivity();
                SdkHelper.onResume(activity);
                EyuAdManager.getInstance().resume(activity);
            }

            @Override
            public void onPause() {
                Activity activity =  NJavaBase.getActivity();
                SdkHelper.onPause(activity);
                EyuAdManager.getInstance().pause(activity);
            }

            @Override
            public void onDestroy() {
                Activity activity =  NJavaBase.getActivity();
                EyuAdManager.getInstance().destroyCurrent(activity);
            }
        });
    }

    /*
    public static void initUmeng()
    {
        SdkHelper.initUmSdk(this, "appKey", "channel");
    }
    */

    public static void initAppFlyer()
    {
        Activity activity =  NJavaBase.getActivity();
        String AF_DEV_KEY="";
        try {
            ApplicationInfo appInfo = activity.getPackageManager().getApplicationInfo(
                    activity.getPackageName(), PackageManager.GET_META_DATA
            );

            AF_DEV_KEY= appInfo.metaData.getString("AF_DEV_KEY");
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        initAppFlyer(AF_DEV_KEY);
    }

    public static void initAppFlyer(String devKey)
    {
        Activity activity =  NJavaBase.getActivity();

        SdkHelper.initAppFlyerSdk(devKey,
                new AppsFlyerConversionListener(){
                    @Override
                    public void onConversionDataSuccess(Map<String, Object> map) {

                    }

                    @Override
                    public void onConversionDataFail(String s) {

                    }

                    @Override
                    public void onAppOpenAttribution(Map<String, String> map) {

                    }

                    @Override
                    public void onAttributionFailure(String s) {

                    }
                },
                activity.getApplication(),
                ""
        );

    }

    //在统计初始化之后
    public static void  initRemoteConfig()
    {
        Activity activity =  NJavaBase.getActivity();
        Map<String, Object> defaultsMap = new HashMap<>();
        defaultsMap.put("key","defaultValue");
        EyuRemoteConfigHelper.initRemoteConfig(activity, defaultsMap);
        EyuRemoteConfigHelper.fetchRemoteConfig();
    }

    //输出日志
    public static void logEvent(String event, Map<String, Object> params)
    {
        EventHelper.logEventMap(event, params);
    }
    //EventHelper.logEventMap(event, dataMap);
    //EventHelper.logEvent(event,"json字符串")

    //在 initRemoteConfig 之后
    public static void initAd()
    {
        Activity activity =  NJavaBase.getActivity();
        AdConfig adConfig = new AdConfig();
        /**
         * 广告位
         * {
         *     "cacheGroup": "main_view_inter_ad",
         *     "isEnabled": "true",
         *     "nativeAdLayout": "",
         *     "id": "main_view_inter_ad",
         *     "desc": "首页插屏"
         *   }
         */
        //ad_setting
        adConfig.setAdPlaceConfigStr(SdkHelper.readRawString(activity, R.raw.and_ad_setting));
        /**
         * 广告key配置
         * [
         * {"id":"adcp_js","key":"ca-app-pub-3940256099942544/1033173712","network":"admob"},
         * {"id":"adjl_jsg","key":"ca-app-pub-3940256099942544/5224354917","network":"admob"},
         * {"id":"adys_sy","key":"ca-app-pub-3940256099942544/2247696110","network":"admob"},
         * {"id":"adys_ba","key":"ca-app-pub-3940256099942544/6300978111","network":"admob"}
         * ]
         */
        adConfig.setAdKeyConfigStr(SdkHelper.readRawString(activity, R.raw.and_ad_key_setting));

        /**
         * 广告位中广告的缓存池，id与广告位id对应
         * keys：广告key配置中的key
         * isAutoLoad：是否自动加载
         * type：广告类型，interstitialAd为插屏
         * {
         *     "keys": "[\"fb_cp_g\"]",
         *     "isAutoLoad": "true",
         *     "id": "main_view_inter_ad",
         *     "type": "interstitialAd"
         *   }
         */
        adConfig.setAdGroupConfigStr(SdkHelper.readRawString(activity, R.raw.and_ad_cache_setting));
//        adConfig.setAdmobClientId("");
//        adConfig.setUnityClientId("");
//        adConfig.setVungleClientId("");
//        adConfig.setTtClientId("5010560");
//        adConfig.setMaxTryLoadRewardAd(1);
//        adConfig.setMaxTryLoadNativeAd(1);
//        adConfig.setMaxTryLoadInterstitialAd(1);

        adConfig.setAdmobClientId("ca-app-pub-9624926763614741~7276924487");

        adConfig.setTestParams(
                TestParams.builder(true)
               .addAdmobTestDevice("45A3A3356CA0CFF6C09303F4A0702286")
        );



        adConfig.setFbNativeAdClickAreaControl(true);
        adConfig.setReportEvent(true);

        //"")
        EyuAdManager.getInstance().config(activity, adConfig, new EyuAdsListener() {
            @Override
            public void onAdReward(String type, String placeId) {
                EyuVideo.I().onRewarded();
                Log.d(Tag,"onAdReward");
            }

            @Override
            public void onAdLoaded(String type, String placeId) {
                EyuVideo.I().onFinishLoadingAD();
                Log.d(Tag,"onAdLoaded");
            }

            @Override
            public void onAdShowed(String type, String placeId) {
                Log.d(Tag,"onAdShowed");
            }

            @Override
            public void onAdClosed(String type, String placeId) {
                EyuVideo.I().onRewardedVideoClose();
                Log.d(Tag,"onAdClosed");
            }

            @Override
            public void onAdClicked(String type, String placeId) {
                Log.d(Tag,"onAdClicked");
            }

            @Override
            public void onDefaultNativeAdClicked() {
                Log.d(Tag,"onDefaultNativeAdClicked");
            }

/*
            public void onAdLoadFailed(String placeId, String key, int code) {
                EyuVideo.I().onFailToLoad();
                Log.d(
                        Tag,
                        String.format("onAdLoadFailed placeId:%s key:%s code:%d",placeId,key,code)
                );
            }*/

            @Override
            public void onAdLoadFailed(String type, String placeId, String key, int code) {
                EyuVideo.I().onFailToLoad();
                Log.d(
                        Tag,
                        String.format("onAdLoadFailed placeId:%s key:%s code:%d",placeId,key,code)
                );
            }


            @Override
            public void onImpression(String type, String placeId) {
                Log.d(Tag,"onImpression");
            }
        });

        getUIDs();
    }

    static void getUIDs()
    {
        /*
        AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    Activity activity =  NJavaBase.getActivity();

                    AdvertisingIdClient.Info adInfo = AdvertisingIdClient.getAdvertisingIdInfo(activity);
                    String myId = adInfo != null ? adInfo.getId() : null;

                    Log.i(Tag,"devid:"+myId);
                } catch (Exception e) {
                   // Toast toast = Toast.makeText(conext, "error occured ", Toast.LENGTH_SHORT);
                   // toast.setGravity(gravity, 0,0);
                   // toast.show();
                    Log.d(Tag,"error occured ");

                }
            }
        });*/
    }

}
